package qlsl.androiddesign.view.subview.fragmentview;

import java.util.ArrayList;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import qlsl.androiddesign.activity.subactivity.TestVpVpActivity;
import qlsl.androiddesign.adapter.baseadapter.FragmentPagerAdapter;
import qlsl.androiddesign.fragment.basefragment.BaseFragment;
import qlsl.androiddesign.fragment.commonfragment.TestVp2Fragment;
import qlsl.androiddesign.fragment.commonfragment.TestVp3Fragment;
import qlsl.androiddesign.fragment.subfragment.TvFragment;
import qlsl.androiddesign.listener.GuidePageChangeListener;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.subview.activityview.TestVpVpView;

/**
 * 里面的viepager页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class TvView extends FunctionView<TestVpVpActivity> implements OnClickListener {

	private TextView[] tv_guides;

	private ImageView iv_cursor;

	public ViewPager viewPager;

	private List<BaseFragment> list;

	TvFragment fragment;

	public static String city = "武汉";

	private GuidePageChangeListener guideListener;

	public TvView(TvFragment fragment, ViewGroup rootView, ViewGroup contentView) {
		super((TestVpVpActivity) fragment.getActivity());
		this.fragment = fragment;
		setContentView(rootView, contentView);
	}

	protected void initView(View view) {
		hideTitleBar();

		tv_guides = new TextView[2];
		tv_guides[0] = (TextView) view.findViewById(R.id.tv_guide1);
		tv_guides[1] = (TextView) view.findViewById(R.id.tv_guide2);
		iv_cursor = (ImageView) view.findViewById(R.id.iv_cursor);
		viewPager = (ViewPager) view.findViewById(R.id.viewPager);
		// viewPager.setOffscreenPageLimit(0);
		setCursorImageMatrix();
	}

	private void setCursorImageMatrix() {
		int bmpW = BitmapFactory.decodeResource(activity.getResources(), R.drawable.iv_cursor).getWidth();
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		int offset = (screenW / tv_guides.length - bmpW) / 2;
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		iv_cursor.setImageMatrix(matrix);

		setViewPagerAdapter(offset, bmpW);
	}

	private void setViewPagerAdapter(int offset, int bmpW) {
		list = new ArrayList<BaseFragment>();
		BaseFragment fragment1 = new TestVp2Fragment();// 出售
		BaseFragment fragment2 = new TestVp3Fragment();// 求购
		list.add(fragment1);
		list.add(fragment2);
		viewPager.setAdapter(new FragmentPagerAdapter(fragment.getChildFragmentManager(), list));
		viewPager.setCurrentItem(0);
		tv_guides[0].setActivated(true);
		guideListener = new GuidePageChangeListener(tv_guides, iv_cursor, offset, bmpW);
		viewPager.setOnPageChangeListener(guideListener);
	}

	protected void initData() {

	}

	protected void initListener() {
		tv_guides[0].setOnClickListener(this);
		tv_guides[1].setOnClickListener(this);
		((TestVpVpView) activity.getFunctionView()).mSlidingTab.setOnPageChangeListener(new PageListener1());
	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_guide1:
			doClickGuideView(view);
			break;
		case R.id.tv_guide2:
			doClickGuideView(view);
			break;
		default:
			int index = viewPager.getCurrentItem();
			list.get(index).onClick(view);
			break;
		}
	}

	private void doClickGuideView(View view) {
		for (int index = 0; index < tv_guides.length; index++) {
			if (view == tv_guides[index]) {
				viewPager.setCurrentItem(index);
				tv_guides[index].setActivated(true);
			} else {
				tv_guides[index].setActivated(false);
			}
		}
	}

	protected class PageListener1 implements OnPageChangeListener {

		public void onPageScrollStateChanged(int arg0) {

		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		public void onPageSelected(int arg0) {
			String[] titles = activity.getResources().getStringArray(R.array.news_viewpage_arrays);
			city = titles[arg0];
			TestVp2View functionView1 = ((TestVp2View) list.get(0).getFunctionView());
			TestVp3View functionView2 = ((TestVp3View) list.get(1).getFunctionView());
			if (functionView1 != null && functionView2 != null) {
				functionView1.query(titles[arg0]);
				functionView2.query(titles[arg0]);
			} else {
				setCursorImageMatrix();
			}
		}
	}

}
